package io.chenglicun.modules.check.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.chenglicun.common.service.impl.CrudServiceImpl;
import io.chenglicun.common.constant.Constant;
import io.chenglicun.modules.check.dao.ClcCheckDao;
import io.chenglicun.modules.check.dao.ClcCheckDetailDao;
import io.chenglicun.modules.check.dao.ClcRectificationDao;
import io.chenglicun.modules.check.dto.ClcCheckDTO;
import io.chenglicun.modules.check.dto.ClcCheckDetailDTO;
import io.chenglicun.modules.check.dto.ClcRectificationDTO;
import io.chenglicun.modules.check.entity.ClcCheckDetailEntity;
import io.chenglicun.modules.check.entity.ClcCheckEntity;
import io.chenglicun.modules.check.entity.ClcRectificationEntity;
import io.chenglicun.modules.check.service.ClcCheckDetailService;
import io.chenglicun.modules.check.service.ClcCheckService;
import io.chenglicun.modules.check.service.ClcRectificationService;
import io.chenglicun.modules.security.user.SecurityUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * 整改审核记录
 *
 * @author HB 137664951@qq.com
 * @since 1.0 2023-11-29
 */
@Service
public class ClcRectificationServiceImpl extends CrudServiceImpl<ClcRectificationDao, ClcRectificationEntity, ClcRectificationDTO> implements ClcRectificationService {

    @Autowired
    private ClcCheckDao checkDao;
    @Autowired
    private ClcCheckDetailDao checkDetailDao;

    @Override
    public QueryWrapper<ClcRectificationEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<ClcRectificationEntity> wrapper = new QueryWrapper<>();


        return wrapper;
    }

    @Override
    @Transactional
    public void save(ClcRectificationDTO dto) {
        super.save(dto);
        // 状态(0: 不通过 1:通过)
        Integer status = dto.getStatus();
        if (status == 0) {
            ClcCheckDetailEntity checkDetailEntity = checkDetailDao.selectById(dto.getCheckDetailId());
            checkDetailEntity.setStatus(0);
            checkDetailDao.updateById(checkDetailEntity);
        } else if (status == 1) {
            ClcCheckDetailEntity checkDetailEntity = checkDetailDao.selectById(dto.getCheckDetailId());
            checkDetailEntity.setStatus(2);
            checkDetailDao.updateById(checkDetailEntity);
            ClcCheckEntity checkEntity = checkDao.selectById(dto.getCheckId());
            checkEntity.setScore(checkEntity.getScore() - checkDetailEntity.getScore());
            checkDao.updateById(checkEntity);
        }
    }
}